const BASE_URL = 'http://124.222.248.97:8000'
// const BASE_URL = 'http://172.20.10.3:8018'
// const BASE_URL = 'http://192.168.1.92:8018'
const request = (options)=> {
	return new Promise((resolve,reject)=>{
		const user= uni.getStorageSync('user')
		if(user.token){
			if(options.header==undefined){
				options['header']={}
			}
			options.header["Authorization"]=user.token
		 }
		uni.request({
			url: BASE_URL+options.url,
			method: options.method || 'GET',
			data: options.data || {},
			header: options.header || {},
			success: (res)=>{
				let data = res.data
				if(data.code !== 200){
					this.$toast.error('后台访问失败');
					reject(new Error(data.message))
				} else {
					resolve(data)
				}
			},
			fail: (err)=>{
				this.$toast.error('后台访问失败');
				reject(err)
			}
		})
	})
}


const sendFormAPI = (param) => {
	return new Promise((resolve, reject) => {
		let header = param.header || {}
		header["Content-Type"] = header["Content-Type"] || 'application/x-www-form-urlencoded'
		request({
			url: param.url,
			method: param.method || 'GET',
			header: header,
			data: param.data || {}
		}).then(response => {
			resolve(response)
		}).catch(error => {
			resolve(error)
		})
	})
}


module.exports = {
	sendFormAPI
}